package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.Stage_wzl;
import com.utils.EntityFactory;
import com.utils.ExecUtil;
import com.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/stage/queryStageList", "/stage/add", "/stage/edit", "/stage/status"})
public class StageService_wzl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/stage/queryStageList")) {
            resp.getWriter().write("403 not allowed");
            return;
        }
        String sql = "select * from yizhan_wzl";
        List<Stage_wzl> stageWzlList = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        Boolean judge = true;
        if (!req.getParameter("stagename").isEmpty()) {
            params.add(req.getParameter("stagename"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " stagename like \"%\"?\"%\"";
        }
        if (!req.getParameter("community").isEmpty()) {
            params.add(req.getParameter("community"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " community like \"%\"?\"%\"";
        }
        if (!req.getParameter("time1").isEmpty()) {
            params.add(req.getParameter("time1"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " time >= ?";
        }
        if (!req.getParameter("time2").isEmpty()) {
            params.add(req.getParameter("time2"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " time <= ?";
        }
        try (ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false)) {
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory<>(Stage_wzl.class);
                Stage_wzl stage_wzl = (Stage_wzl) entityFactory.startCombine(resultSet);
                stageWzlList.add(stage_wzl);
            }
            resp.getWriter().write(JSON.toJSONString(stageWzlList));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql;
        List<Object> params = new ArrayList<>();
        String data;
        if (!req.getServletPath().equals("/stage/add") && !req.getServletPath().equals("/stage/edit") && !req.getServletPath().equals("/stage/status")) {
            resp.getWriter().write(Result.fail(404, "网址错误"));
            return;
        }
        if (req.getServletPath().equals("/stage/add")) {
            sql = "insert into yizhan_wzl(stagename,stageaddress,community,time) values(?,?,?,?)";
            params.add(req.getParameter("stagename"));
            params.add(req.getParameter("stageaddress"));
            params.add(req.getParameter("community"));
            params.add(new Date());
            data = "添加成功";
        } else if (req.getServletPath().equals("/stage/edit")) {
            sql = "update yizhan_wzl set stagename=?,stageaddress=?,community=? where id = ?";
            params.add(req.getParameter("stagename"));
            params.add(req.getParameter("stageaddress"));
            params.add(req.getParameter("community"));
            params.add(req.getParameter("id"));
            data = "编辑成功";
        } else {
            sql = "select status from yizhan_wzl where id = ?";
            List<Object> params_query = new ArrayList<>();
            params_query.add(req.getParameter("id"));
            try (ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params_query, req, false)) {
                resultSet.next();
                Integer status = resultSet.getInt("status");
                if (status == 1) {
                    sql = "update yizhan_wzl set status = 0 where id = ?";
                    params.add(req.getParameter("id"));
                } else {
                    sql = "update yizhan_wzl set status = 1 where id = ?";
                    params.add(req.getParameter("id"));
                }
                data = "修改成功";
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        try {
            ExecUtil.execute(sql, params, req, true);
            resp.getWriter().write(Result.success(data));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
